package algotithm.jianzhioffer.test49;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/15-23:24
 * @URL https://leetcode-cn.com/problems/chou-shu-lcof/
 * @Title 剑指 Offer 49. 丑数
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int n = 10;
        int number = solution.nthUglyNumber(n);
        System.out.println(number);
    }

    public int nthUglyNumber(int n) {
        int[] dp = new int[n];
        int a = 0, b = 0, c = 0;
        dp[0] = 1;
        for (int i = 1; i < n; i++) {
            int n2 = dp[a] * 2;
            int n3 = dp[b] * 3;
            int n5 = dp[c] * 5;
            dp[i] = Math.min(Math.min(n2,n3),n5);
            if (n2 == dp[i]) {
                a++;
            }
            if (n3 == dp[i]) {
                b++;
            }
            if (n5 == dp[i]) {
                c++;
            }
        }
        return dp[n-1];
    }
}
